package socratesteam.view.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import socratesteam.view.common.constantes.LoginConstants;


public class FilterSession implements Filter{

	FilterConfig fc;
	
	public void destroy() {		
		fc=null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res =(HttpServletResponse)response;
		HttpSession session = req.getSession(true);
		
		String paginaRequerida=req.getRequestURL().toString();
		
		if( session.getAttribute(LoginConstants.USUARIO) == null
				&& !paginaRequerida.contains("login.jsfx")){
			res.sendRedirect("../../index.jsp");		
			//System.out.println(paginaRequerida);
		}else{		
			if( session.getAttribute(LoginConstants.USUARIO) != null
					&& paginaRequerida.contains("login.jsfx")){
				res.sendRedirect("principal.jsfx");	
			}
			//System.out.println(paginaRequerida);
			chain.doFilter(req, res);
		}		
		
	}

	public void init(FilterConfig filterConfig) throws ServletException {		
		fc=filterConfig;
	}

}
